package com.lanou.lenanapp.adapter;

import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.lanou.lenanapp.R;
import com.lanou.lenanapp.beans.JokeReview;
import com.lanou.lenanapp.view.CircleImage;
import com.lanou.lenanapp.volley.VolleySingleton;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by dllo on 15/10/22.
 */
public class JokeReviewNewAdapter extends BaseAdapter{

    private TextView hot_joke_review_author_name;
    private CircleImage hot_joke_review_author_avatar;
    private TextView hot_joke_review_created_time;
    private TextView hot_joke_review_content;
    private TextView hot_joke_review_likes;
    private List<JokeReview> newJokeReviews;
    private Object bitMap;

    VolleySingleton volleySingleton = VolleySingleton.getInstance();



    public void addData(List<JokeReview> datas){
        newJokeReviews = datas;
        notifyDataSetChanged();
    }

    /****
     * 根据数据集合个数返回
     * 控制item条数 即控制列表个数
     * ***/
    @Override
    public int getCount() {
        //根据数据集合长度返回列表个数
        return newJokeReviews != null && newJokeReviews.size() > 0 ? newJokeReviews.size() : 0;
    }


    /**
     * 返回当前下表对应的数据
     * **/
    @Override
    public Object getItem(int position) {
        return null;
    }


    /**
     * 返回当前下标对应的id
     * **/
    @Override
    public long getItemId(int position) {
        return 0;
    }



    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView == null){
            //获得布局加载器
            LayoutInflater inflater = LayoutInflater.from(parent.getContext());
            //加载行布局
            convertView = inflater.inflate(R.layout.activity_review_joke_new, null);
        }
        hot_joke_review_author_avatar = (CircleImage) convertView.findViewById(R.id.hot_joke_review_author_avatar);
         hot_joke_review_author_name = (TextView) convertView.findViewById(R.id.hot_joke_review_author_name);
         hot_joke_review_created_time = (TextView) convertView.findViewById(R.id.hot_joke_review_created_time);
        hot_joke_review_content = (TextView) convertView.findViewById(R.id.hot_joke_review_content);
         hot_joke_review_likes = (TextView) convertView.findViewById(R.id.hot_joke_review_likes);

        if (newJokeReviews != null && newJokeReviews.size() > 0){
            JokeReview j = newJokeReviews.get(position);
            //时间转换
            String time = getNormalTime(j.getCreated_time());
            hot_joke_review_created_time.setText(time);

            hot_joke_review_content.setText(j.getContent());
            hot_joke_review_likes.setText(j.getLikes());
            hot_joke_review_author_name.setText(j.getAuthor_name());
            //设置图片
            String url = j.getAuthor_avatar();
            getBitmap(url, hot_joke_review_author_avatar);

        }

        return convertView;
    }

    /***
     * 时间格式转换
     * **/
    public  String  getNormalTime( String value) {
        Long l = Long.valueOf(value);
        l = l * 1000;
        Date date = new Date(l);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") ;
        String time = format.format(date) ;
        return time;
    }



    private void getBitmap(String url, final ImageView imageView) {
        final ImageRequest request = new ImageRequest(url, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {
                imageView.setImageBitmap(response);
            }
        }, 1000, 1000, Bitmap.Config.ARGB_8888, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });
        volleySingleton.addRequest(request);
    }


}
